<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2018/12/21
 * Time: 14:16
 */

namespace app\admin\controller;


use think\Db;

class Product extends Base
{
    public $table = 'course';

    public function index()
    {
        $keyword = input('keyword');
        $column_id = input('column_id/d');
        $difficulty_id = input('difficulty_id/d');
        if($keyword)
            $where['a.name'] = ['like','%'.$keyword.'%'];
        if($column_id)
            $where['a.column_id'] = ['eq',$column_id];
        if($difficulty_id)
            $where['a.difficulty_id'] = ['eq',$difficulty_id];
        if(!isset($where))
            $where = '1=1';
        $data['list'] = Db::name($this->table)
            ->alias('a')
            ->join('column b','b.id=a.column_id','left')
            ->join('difficulty c','c.id=a.difficulty_id','left')
            ->where($where)
            ->order('a.id desc')
            ->field('a.*,b.name as column_name,c.name as difficulty_name')
            ->paginate();
        $data['page'] = $data['list']->render();

        $config = $this->getAllType();

        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->assign('column_id',$column_id);
        $this->assign('difficulty_id',$difficulty_id);
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }

    public function add()
    {
        if(request()->isPost()){
            return $this->save();
        }
        $this->assign('config',json_encode($this->getAllType()));
        $this->assign('data',1);
        $this->assign('title','新增');
        $this->assign('url',url('add'));
        return $this->fetch('info');
    }

    public function update()
    {
        $id = input('id/d');
        $data = Db::name($this->table)->where('id',$id)->find();
        if(!$data){
            $this->error('不存在');
        }
        if(request()->isPost()){
            return $this->save();
        }
        $data['use_id'] = $data['use_id']?explode(',',$data['use_id']):[];
        $this->assign('config',json_encode($this->getAllType()));
        $this->assign('data',json_encode($data));
        $this->assign('title','编辑');
        $this->assign('url',url('update'));
        return $this->fetch('info');
    }

    public function save()
    {
        $id = input('id/d');
        $param = input();
        $data = [
            'name'=>input('name'),
            'des'=>input('des'),
            'column_id'=>input('column_id'),
            'use_id'=>empty($param['use_id'])?'':join(',',$param['use_id']),
            'difficulty_id'=>input('difficulty_id'),
            'color'=>input('color'),
            'head_img'=>input('head_img')?:'',
            'sort'=>input('sort/d'),
            'display'=>input('display/d'),
            'modtime'=>time(),
        ];
        if($id){
            $a = Db::name($this->table)->where('id',$id)->update($data);
        }else{
            $data['addtime'] = time();
            $data['nums'] = 0;
            $data['member_id'] = 0;
            $data['admin_id'] = $this->admin['id'];
            $a = Db::name($this->table)->insert($data);
        }
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
        return;
    }

    public function ajaxupdate()
    {
        $type = input('type');
        $field = input('field');
        $data[$field] = input('value/d');
        $id = input('id/d');
        $data['modtime'] = time();
        $g = Db::name($this->table)->where('id',$id)->find();
        if(!$g)
            return dataReturn(0,'不存在');
        if($field == 'extra'){
            $data['total'] = $g['sale'] + $data['extra'];
        }
        $a = Db::name($this->table)->where('id',$id)->update($data);
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
    }

    public function getAllType()
    {
        $config['difficulty'] = Db::name('difficulty')->select();
        $config['use'] = Db::name('use')->select();
        $config['column'] = Db::name('column')->select();
        $config['color'] = config('app.course_color');
        return $config;
    }

}